package com.zyh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyh.dao.JarMapper;
import com.zyh.entity.Jar;
import com.zyh.service.JarService;
import com.zyh.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class JarServiceImpl implements JarService {
    private SqlSession sqlSession = null;
    @Override
    public void insertOneJar(Jar jar) {
        sqlSession = MybatisUtils.getSqlSession();
        JarMapper jarMapper = sqlSession.getMapper(JarMapper.class);

        try {
            jarMapper.insertOneJar(jar);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
    }

    @Override
    public void deleteOneJar(Integer id) {

    }

    @Override
    public List<Jar> selectAll() {
        sqlSession = MybatisUtils.getSqlSession();
        JarMapper jarMapper = sqlSession.getMapper(JarMapper.class);
        List<Jar> jarList = null;
        try {
            jarList = jarMapper.selectAllJarByPage("");
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
        return jarList;
    }

    @Override
    public Jar selectOne(int id) {
        sqlSession = MybatisUtils.getSqlSession();
        JarMapper jarMapper = sqlSession.getMapper(JarMapper.class);
        Jar jar = null;
        try {
            jar = jarMapper.selectOne(id);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
        return jar;
    }

    @Override
    public List<Jar> selectJars(int[] ids) {
        sqlSession = MybatisUtils.getSqlSession();
        JarMapper jarMapper = sqlSession.getMapper(JarMapper.class);
        List<Jar> jarList = null;
        try {
            jarList = jarMapper.selectJars(ids);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
        return jarList;
    }

    @Override
    public void deleteJars(int[] ids) {
        sqlSession = MybatisUtils.getSqlSession();
        JarMapper jarMapper = sqlSession.getMapper(JarMapper.class);

        try {
            jarMapper.deleteJars(ids);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
    }

    @Override
    public void updateOneJar(Jar jar) {

    }

    @Override
    public PageInfo<Jar> selectAllJarByPage(Integer curPage,Integer pageSize,String nameSearch) {
        sqlSession = MybatisUtils.getSqlSession();
        JarMapper jarMapper = sqlSession.getMapper(JarMapper.class);
        PageInfo<Jar> jarPageInfo = null;
        List<Jar> jarList = null;

        try {
            PageHelper.startPage(curPage,pageSize);
            jarList = jarMapper.selectAllJarByPage(nameSearch);
            jarPageInfo = new PageInfo<>(jarList);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtils.closeSqlSession(sqlSession);
        }
        return jarPageInfo;
    }
}
